import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;

import org.freeswitch.*;
import org.freeswitch.swig.*;


public class Common {

	
	//parameters of path.lua file
	
	int MESSAGE_STATUS_PENDING = 0;
	int MESSAGE_STATUS_APPROVED = 1;
	int MESSAGE_STATUS_REJECTED = 2;

	int FILTER_CODE_ALL_ONLY = 0;
	int FILTER_CODE_ALL_FIRST = 1;
	int FILTER_CODE_NO_ALL = 2;
	int FILTER_CODE_ALL_LAST = 3;

	int GLOBAL_JUMP_MESSAGE=9;

	String GLOBAL_MENU_MAINMENU = "0";
	String GLOBAL_MENU_NEXT = "1";
	String GLOBAL_MENU_RESPOND = "2";
	String GLOBAL_MENU_INSTRUCTIONS = "3";
	String GLOBAL_MENU_SKIP_BACK = "4";
	String GLOBAL_MENU_PAUSE = "5";
	String GLOBAL_MENU_SKIP_FWD = "6";
	String GLOBAL_MENU_SEEK_BACK = "7";
	String GLOBAL_MENU_REPLAY = "8";
	String GLOBAL_MENU_SEEK_FWD = "9";
	
	String basedir="//usr//local//freeswitch";
	String temp="//sound//en//us//callie//";
	
	String bsd=basedir+temp;	
    
	String sd="//home//ivr//media//WS";
	
	String logfilename="//home//ivr//Documents//Log//IIITD//IIITD.log";
	
	
	Optimized obj=null;
	//private OutputStreamWriter logfile;
	
	public Common(Optimized o)
	{
		this.obj=o;
	}
	
	public void hangup() throws Exception
	{
		//garbage collector in java finalize method of each class
		obj.con.close();
		//dont knw from where this env has come
		//env.close();
		
		//needs the string argument
		obj.session.hangup("Normal_Clearing");
	}
	
	//not implemented rows and row function already have done sql statement work
	
	public long ostime()
	{
		Date date=new Date();
		return(date.getTime());
	}
	
	public void sleep(int delay) throws Exception
	{
		read("",delay);
	}
	public void read(String file,int delay) throws Exception
	{
		PrintWriter logfile=new PrintWriter(new FileWriter(logfilename));
		
		logfile.write(obj.sessid+"\t"+obj.session.getVariable("caller_id_number")+"\t"+obj.session.getVariable("destination_number")+"\t"+ostime()+"\t"+"Currently at node:"+"\t"+obj.input+"\n");
		
		if(obj.digits.compareTo("")==0)
		{
			logfile.write(obj.sessid+"\t"+obj.session.getVariable("caller_id_number")+"\t"+obj.session.getVariable("destination_number")+"\t"+ostime()+"\t"+"dtmf"+"\t"+file+"\t"+obj.digits+"\n");
			//logfile.write(obj.sessid+"\t"+obj.session.getVariable("caller_id_number")+"\t"+obj.session.getVariable("destination_number")+"\t"+ostime()+"\t"+"dtmf"+"\t"+file+"\t"+obj.digits+"\n");
			
			//dont know whether this change the value of digits in main program or not
			
			obj.digits=obj.session.read(1, 1, file, delay, "#", 0);
			
			
		}
		
		if(obj.digits.compareTo("")!=0)
		{
			logfile.write(obj.sessid+"\t"+obj.session.getVariable("caller_id_number")+"\t"+obj.session.getVariable("destination_number")+"\t"+ostime()+"\t"+"dtmf"+"\t"+file+"\t"+obj.digits+"\n");
			
		}
		
		
	}
	
	
	public String use()
	{
		String d=obj.digits;
		//find whether digits value will change or not
		obj.digits="";
		obj.session.flushDigits();
		return d;
	}
}
